
const _interopRequireDefault = require('@babel/runtime/helpers/interopRequireDefault');

exports.__esModule = true;
exports.default = void 0;

const _babelHelperVueJsxMergeProps = _interopRequireDefault(require('@vue/babel-helper-vue-jsx-merge-props'));

const _utils = require('../utils');

const _functional = require('../utils/functional');

const _icon = _interopRequireDefault(require('../icon'));

const _sidebar = _interopRequireDefault(require('../sidebar'));

const _sidebarItem = _interopRequireDefault(require('../sidebar-item'));

// Utils
// Components
const _createNamespace = (0, _utils.createNamespace)('tree-select');
const createComponent = _createNamespace[0];
const bem = _createNamespace[1];

function TreeSelect(h, props, slots, ctx) {
  const { height } = props;
  const { items } = props;
  const { mainActiveIndex } = props;
  const { activeId } = props;
  const selectedItem = items[+mainActiveIndex] || {};
  const subItems = selectedItem.children || [];
  const isMultiple = Array.isArray(activeId);

  function isActiveItem(id) {
    return isMultiple ? activeId.indexOf(id) !== -1 : activeId === id;
  }

  const Navs = items.map((item) => h(_sidebarItem.default, {
    attrs: {
      dot: item.dot,
      info: (0, _utils.isDef)(item.badge) ? item.badge : item.info,
      title: item.text,
      disabled: item.disabled,
    },
    class: [bem('nav-item'), item.className],
  }));

  function Content() {
    if (slots.content) {
      return slots.content();
    }

    return subItems.map((item) => h('div', {
      key: item.id,
      class: ['van-ellipsis', bem('item', {
        active: isActiveItem(item.id),
        disabled: item.disabled,
      })],
      on: {
        click: function click() {
          if (!item.disabled) {
            let newActiveId = item.id;

            if (isMultiple) {
              newActiveId = activeId.slice();
              const index = newActiveId.indexOf(item.id);

              if (index !== -1) {
                newActiveId.splice(index, 1);
              } else if (newActiveId.length < props.max) {
                newActiveId.push(item.id);
              }
            }

            (0, _functional.emit)(ctx, 'update:active-id', newActiveId);
            (0, _functional.emit)(ctx, 'click-item', item); // compatible with legacy usage, should be removed in next major version

            (0, _functional.emit)(ctx, 'itemclick', item);
          }
        },
      },
    }, [item.text, isActiveItem(item.id) && h(_icon.default, {
      attrs: {
        name: 'success',
      },
      class: bem('selected'),
    })]));
  }

  return h('div', (0, _babelHelperVueJsxMergeProps.default)([{
    class: bem(),
    style: {
      height: (0, _utils.addUnit)(height),
    },
  }, (0, _functional.inherit)(ctx)]), [h(_sidebar.default, {
    class: bem('nav'),
    attrs: {
      activeKey: mainActiveIndex,
    },
    on: {
      change: function change(index) {
        (0, _functional.emit)(ctx, 'update:main-active-index', index);
        (0, _functional.emit)(ctx, 'click-nav', index); // compatible with legacy usage, should be removed in next major version

        (0, _functional.emit)(ctx, 'navclick', index);
      },
    },
  }, [Navs]), h('div', {
    class: bem('content'),
  }, [Content()])]);
}

TreeSelect.props = {
  max: {
    type: [Number, String],
    default: Infinity,
  },
  items: {
    type: Array,
    default: function _default() {
      return [];
    },
  },
  height: {
    type: [Number, String],
    default: 300,
  },
  activeId: {
    type: [Number, String, Array],
    default: 0,
  },
  mainActiveIndex: {
    type: [Number, String],
    default: 0,
  },
};

const _default2 = createComponent(TreeSelect);

exports.default = _default2;
